package com.kzd.sdk.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;

import android.os.Handler;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kzd.sdk.core.KZD_SdkController;
import com.kzd.sdk.entity.KZD_LoginMain;
import com.kzd.sdk.ui.activity.KZD_HomeActivity;
import com.kzd.sdk.ui.activity.KZD_UserServiceActivity;
import com.kzd.sdk.ui.activity.KZD_WebRichActivity;
import com.kzd.sdk.utils.AppUtils;

import static android.content.Context.WINDOW_SERVICE;

@SuppressLint("ClickableViewAccessibility")
public class KZD_FloatView extends LinearLayout implements View.OnTouchListener {

    private WindowManager mWindowManager;

    private WindowManager.LayoutParams mWmLayoutParams;

    private ImageView mFloatMenuView, mPersonalCenterView, mServiceCenterView;

    private final Handler mHandler = new Handler();

    private final Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            mFloatMenuView.setTranslationX(AppUtils.dp2px(27f) * -1);
        }
    };

    public KZD_FloatView(Context context) {
        super(context);
        initManageAndView(context);
    }

    private void initManageAndView(Context context) {

        mWindowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
        mWmLayoutParams = new WindowManager.LayoutParams();
        mWmLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
        mWmLayoutParams.format = PixelFormat.RGBA_8888;
        mWmLayoutParams.gravity = Gravity.TOP | Gravity.START;
        mWmLayoutParams.flags = 262696;//WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
        //WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mWmLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mWmLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mWmLayoutParams.y = AppUtils.getAppScreenHeight(context) / 3;

        setGravity(Gravity.CENTER | Gravity.START);
        //创建悬浮菜单视图
        mFloatMenuView = new ImageView(context);
//        LinearLayout.LayoutParams floatMenuParams = new LinearLayout
//                .LayoutParams(AppUtils.dp2px(60f), AppUtils.dp2px(60f));
        LinearLayout.LayoutParams floatMenuParams = new LinearLayout
                .LayoutParams(-2, -2);
        mFloatMenuView.setLayoutParams(floatMenuParams);
        mFloatMenuView.setImageResource(AppUtils.getDrawableId(context, "kzd_float_menu"));
        mFloatMenuView.setScaleType(ImageView.ScaleType.FIT_XY);
        mFloatMenuView.setTranslationX(AppUtils.dp2px(27f) * -1);
        addView(mFloatMenuView);

        //创建悬浮个人中心视图
        mPersonalCenterView = new ImageView(context);
        LinearLayout.LayoutParams personalCenterParams = new LinearLayout
//                .LayoutParams(AppUtils.dp2px(55f), AppUtils.dp2px(55f));
                .LayoutParams(-2, -2);
        personalCenterParams.leftMargin = AppUtils.dp2px(10f);
        mPersonalCenterView.setLayoutParams(personalCenterParams);
        mPersonalCenterView.setImageResource(AppUtils.getDrawableId(context, "kzd_float_btn_usercenter_normal"));
        mPersonalCenterView.setScaleType(ImageView.ScaleType.FIT_XY);
        mPersonalCenterView.setVisibility(GONE);
        addView(mPersonalCenterView);

        //创建悬浮客服中心视图
        mServiceCenterView = new ImageView(context);
        LinearLayout.LayoutParams serviceCenterParams = new LinearLayout
//                .LayoutParams(AppUtils.dp2px(55f), AppUtils.dp2px(55f));
                .LayoutParams(-2, -2);
        serviceCenterParams.leftMargin = AppUtils.dp2px(15f);
        mServiceCenterView.setLayoutParams(serviceCenterParams);
        mServiceCenterView.setImageResource(AppUtils.getDrawableId(context, "kzd_float_btn_kefucenter_normal"));
        mServiceCenterView.setScaleType(ImageView.ScaleType.FIT_XY);
        mServiceCenterView.setVisibility(GONE);
        addView(mServiceCenterView);

        //菜单触碰事件
        mFloatMenuView.setOnTouchListener(this);
        //菜单点击事件
        mFloatMenuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isOpenCenterView(!isOpen);
            }
        });
        //个人中心点击事件
        mPersonalCenterView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                KZD_LoginMain loginMain = KZD_SdkController.getLoginMain();
                if (loginMain == null) {
                    return;
                }
                if (loginMain.getIsH5() == 1) {
                    KZD_WebRichActivity.openUrl("", loginMain.getUrl());
                } else {
                    KZD_HomeActivity.open();
                }
            }
        });
        //客服中心点击事件
        mServiceCenterView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                KZD_UserServiceActivity.open(context);
            }
        });
    }

    public void openFloatView() {
        if (getWindowToken() == null) {
            mWindowManager.addView(this, mWmLayoutParams);
        } else {
            mWindowManager.updateViewLayout(this, mWmLayoutParams);
        }
    }

    public void closeFloatView() {
        if (getWindowToken() == null) {
            return;
        }
        mHandler.removeCallbacks(mRunnable);
        mWindowManager.removeViewImmediate(this);
    }

    private void isOpenCenterView(boolean isOpen) {
        if (isOpen) {
            openCenterView();
        } else {
            closeCenterView();
        }
        this.isOpen = isOpen;
    }

    private void openCenterView() {
        mPersonalCenterView.setVisibility(VISIBLE);
        mServiceCenterView.setVisibility(VISIBLE);
        mHandler.removeCallbacks(mRunnable);
    }

    private void closeCenterView() {
        mPersonalCenterView.setVisibility(GONE);
        mServiceCenterView.setVisibility(GONE);
        mHandler.postDelayed(mRunnable, (3 * 1000));
    }

    private void downCenterView() {
        mFloatMenuView.setTranslationX(0);
        mPersonalCenterView.setVisibility(GONE);
        mServiceCenterView.setVisibility(GONE);
        mHandler.removeCallbacks(mRunnable);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isOpen) {
            isOpenCenterView(false);
        }
        return super.onTouchEvent(event);
    }

    private boolean isDrag = false;
    private boolean isOpen = false;
    private int lastX;
    private int lastY;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isDrag = false;
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                downCenterView();
                break;
            case MotionEvent.ACTION_MOVE:
                int nowX = (int) event.getRawX();
                int nowY = (int) event.getRawY();
                int movedX = nowX - lastX;
                int movedY = nowY - lastY;
                if (Math.abs(lastX - nowX) > 3 && Math.abs(lastY - nowY) > 3) {
                    isDrag = true;
                    lastX = nowX;
                    lastY = nowY;
                    mWmLayoutParams.x = mWmLayoutParams.x + movedX;
                    mWmLayoutParams.y = mWmLayoutParams.y + movedY;
                    mWindowManager.updateViewLayout(KZD_FloatView.this, mWmLayoutParams);
                } else {
                    isDrag = false;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mWmLayoutParams.x = 0;
                if (mWmLayoutParams.y < AppUtils.dp2px(40)) {
                    mWmLayoutParams.y = AppUtils.dp2px(40);
                }
                mWindowManager.updateViewLayout(KZD_FloatView.this, mWmLayoutParams);
                if (!isDrag) {
                    return false;
                }
                isOpen = false;
                mHandler.postDelayed(mRunnable, (3 * 1000));
                return true;
            default:
                return false;
        }
        return false;
    }
}
